//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package server;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.core.server.resources.CoapExchange;
import ui.Simulator;
import util.HexParser;

public class MyResource extends CoapResource {
    byte[] data = null;

    public MyResource(String name) {
        super(name);
        this.setObservable(true);
        this.getAttributes().setTitle("Observable resource");
        this.getAttributes().addResourceType("observe");
        this.getAttributes().setObservable();
        this.setObserveType(Type.CON);
    }

    public void handleGET(CoapExchange exchange) {
        Response response = new Response(ResponseCode.CONTENT);
        response.setPayload(this.data);
        Simulator.getInstance().printConsole("ue payload is %s", new Object[]{HexParser.parseByte2HexStr(response.getPayload())});
        Simulator.getInstance().printConsole("in get addr is %s. port is %s", new Object[]{exchange.getSourceAddress(), exchange.getSourcePort()});
        exchange.respond(response);
    }

    public void handlePOST(CoapExchange exchange) {
        String payload = new String(exchange.getRequestPayload(), StandardCharsets.UTF_8);
        Simulator.getInstance().printConsole("cig payload is %s", new Object[]{payload});
        Simulator.getInstance().printConsole("srcAdd is %s, port is %s, code is %s, options is %s.", new Object[]{exchange.getSourceAddress(), exchange.getSourcePort(), exchange.getRequestCode(), exchange.getRequestOptions()});
        exchange.respond(ResponseCode.CHANGED);
    }

    public void changed() {
        super.changed();
    }

    public void setData(int msgType, String payload) {
        if (null != payload && payload.length() != 0) {
            switch(msgType) {
                case 0:
                    this.data = HexParser.parseHexStr2Byte(payload);
                    break;
                case 1:
                    this.data = this.convertMsgToByte(payload);
                    break;
                default:
                    this.data = HexParser.parseHexStr2Byte(payload);
            }

        } else {
            Simulator.getInstance().printConsole("empty payload.", new Object[0]);
        }
    }

    public void stateChanged(int msgType, String payload) {
        if (null == payload) {
            Simulator.getInstance().printConsole("Error: empty payload.", new Object[0]);
        } else {
            switch(msgType) {
                case 0:
                    this.data = HexParser.parseHexStr2Byte(payload);
                    break;
                case 1:
                    this.data = this.convertMsgToByte(payload);
                    break;
                default:
                    this.data = HexParser.parseHexStr2Byte(payload);
            }

            this.changed();
        }
    }

    private byte[] convertMsgToByte(String payload) {
        try {
            return payload.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
